package com.bdqn.util;

import com.bdqn.entity.util.Constants;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

@Data
//@AllArgsConstructor
//@NoArgsConstructor
public class OrderUtil {

    private static Integer initValue = 1000;
    private static Integer maxValue = 9999;
    private static final AtomicInteger SEQ = new AtomicInteger(initValue);
    private static final DateTimeFormatter DF_FMT_PREFIX = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSS");
    private static ZoneId ZONE_ID = ZoneId.of("Asia/Shanghai");

    //创建订单编号 时间戳+四位数字(1000~9999)
    public static String generateOrderNo() {
        LocalDateTime dataTime = LocalDateTime.now(ZONE_ID);
        if (SEQ.intValue() > maxValue) {
            SEQ.getAndSet(initValue);
        }
        return dataTime.format(DF_FMT_PREFIX) + SEQ.getAndIncrement();
    }

    // 创建超时时间
    public static Date generateOrderExpirationTime() {
        long currentTime = System.currentTimeMillis() + Constants.Time.ORDER_KEEP_TIME;
        Date date = new Date(currentTime);
        return date;
    }
}
